home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / ScriptExplorer / CLG_CharsGrid.cp < prev    next >
Encoding:
Text File  |  1997-07-24  |  9.3 KB  |  365 lines  |  [TEXT/CWIE]

  1. // CLG_CharsGrid.cp
  2. //
  3. //  Displays a 256 character array on screen. This can be single
  4. //    or Double Byte data.
  5. //
  6. //  Maintain 2 local arrays of chars. One is untouched, the other
  7. //    contains the result of the Script Action currently selected.
  8. //
  9. //****************************************************************************
  10.  
  11. #ifdef PowerPlant_PCH
  12. #include PowerPlant_PCH
  13. #endif
  14.  
  15. // system headers
  16. #include <Script.h>
  17. #include <TextUtils.h>
  18.  
  19. // PowerPlant headers
  20. #include <LStream.h>
  21. #include <UDrawingState.h>
  22.  
  23. // project headers
  24. #include "LG_Constants.h"
  25. #include "CLG_CharsGrid.h"
  26.  
  27.  
  28. //***********************
  29. //    CreateDisplayStream
  30. //****************************************************************************
  31. CLG_CharsGrid*
  32. CLG_CharsGrid::CreateDisplayStream(
  33.     LStream    *inStream)
  34. {
  35.     return (new CLG_CharsGrid(inStream));
  36. }
  37.  
  38.  
  39. //***********************
  40. //    CLG_CharsGrid
  41. //****************************************************************************
  42. CLG_CharsGrid::CLG_CharsGrid(void)
  43. {
  44.     this->Init();
  45.     return;
  46. }
  47.  
  48. //***********************
  49. //    CLG_CharsGrid
  50. //****************************************************************************
  51. CLG_CharsGrid::CLG_CharsGrid( LStream *inStream)
  52.     : LPane( inStream)
  53. {
  54.     this->Init();
  55.     return;
  56. }
  57.  
  58. //***********************
  59. //    Init
  60. //****************************************************************************
  61. void CLG_CharsGrid::Init(void)
  62. {
  63.     mBaseChar = 0;
  64.     mFontNum = -1;
  65.     mFontSiz = 14;
  66.  
  67.     mCurScriptNum = smRoman;
  68.     mScriptAction = saNoChange;
  69.  
  70.     FillCodePts();
  71.  
  72.     return;
  73. }
  74.  
  75.  
  76. //***********************
  77. //    FillCodePts
  78. //     Relies on mBaseChar to be valid
  79. //****************************************************************************
  80. void CLG_CharsGrid::FillCodePts(void)
  81. {
  82.     short cnt;
  83.     
  84.     if ( mBaseChar == 0) {
  85.         for ( cnt=0;cnt<256;cnt++) {
  86.             mCodePts[ cnt] = cnt;
  87.         }
  88.     } else {
  89.         for ( cnt=0;cnt<256;cnt++) {
  90.             mCodePts[ cnt] = (mBaseChar<<8) + cnt;
  91.         }
  92.     }
  93.     BlockMoveData( mCodePts, mChars, sizeof(unsigned short)*256);
  94.  
  95.     return;
  96. }
  97.  
  98.  
  99. //***********************
  100. //    ClickSelf
  101. //****************************************************************************
  102. void
  103. CLG_CharsGrid::ClickSelf( const SMouseDownEvent &inMouseDown)
  104. {
  105.     return;
  106. }
  107.  
  108.  
  109. //***********************
  110. //    AdjustCursorSelf
  111. //****************************************************************************
  112. void
  113. CLG_CharsGrid::AdjustCursorSelf(
  114.     Point                inPortPt ,
  115.     const EventRecord&    inMacEvent)
  116. {
  117.     Point    tPt;
  118.     static Rect savR = {0,0,0,0};
  119.  
  120.     
  121.     tPt = inPortPt;
  122.     
  123.     //Debugger();
  124.     
  125.     CursHandle    theCursH = ::GetCursor( 2);
  126.     if (theCursH != nil) {
  127.         ::SetCursor(*theCursH);
  128.     }
  129. }
  130.  
  131.  
  132. //***********************
  133. //    DrawSelf
  134. //****************************************************************************
  135. //  Why do I have to offset these locations?
  136.  
  137. void CLG_CharsGrid::DrawSelf(void)
  138. {
  139.     SDimension16    tFrame;
  140.     Rect            tR;
  141.     short            cntH, cntV;
  142.  
  143.     GetFrameSize( tFrame);
  144.  
  145.     PenNormal();
  146.     MoveTo( 0, mFrameLocation.v+1 );
  147.     Line( tFrame.width, 0);
  148.     MoveTo( 0, mFrameLocation.v+tFrame.height-1 );
  149.     Line( tFrame.width, 0);
  150.  
  151.     TextFont( mFontNum);        TextSize( mFontSiz);    
  152.     TextFace(0);                TextMode( srcOr);
  153.  
  154.     PenPat( &qd.gray);
  155.     PenMode( patOr);
  156.     
  157.     for ( cntV=0; cntV<=16; cntV++) {
  158.         MoveTo( mFrameLocation.h+ kcgHOffset, mFrameLocation.v+ kcgVOffset+ kcgVSpacing*cntV);
  159.         Line( 16*kcgHSpacing, 0);
  160.     }
  161.     for ( cntH=0; cntH<=16; cntH++) {
  162.         MoveTo( mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH, mFrameLocation.v+ kcgVOffset);
  163.         Line( 0, 16*kcgVSpacing);
  164.     }
  165.     // Add informative gray spots for Kanji only
  166.     if ( mBaseChar != 0 && mCurScriptNum == smJapanese) {
  167.         
  168.         for ( cntH=0; cntH<16; cntH++) {
  169.             tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  170.             tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  171.             tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*0 +2;
  172.             tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*1 -1;
  173.             FillRect( &tR, &qd.gray);
  174.         }
  175.         for ( cntH=0; cntH<16; cntH++) {
  176.             tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  177.             tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  178.             tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*1 +2;
  179.             tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*2 -1;
  180.             FillRect( &tR, &qd.gray);
  181.         }
  182.         for ( cntH=0; cntH<16; cntH++) {
  183.             tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  184.             tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  185.             tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*2 +2;
  186.             tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*3 -1;
  187.             FillRect( &tR, &qd.gray);
  188.         }
  189.         for ( cntH=0; cntH<16; cntH++) {
  190.             tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  191.             tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  192.             tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*3 +2;
  193.             tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*4 -1;
  194.             FillRect( &tR, &qd.gray);
  195.         }
  196.         
  197.         cntH = 15;
  198.         tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  199.         tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  200.         tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*7 +2;
  201.         tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*8 -1;
  202.         FillRect( &tR, &qd.gray);
  203.  
  204.         cntH = 13;
  205.         tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  206.         tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  207.         tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*15 +2;
  208.         tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*16 -1;
  209.         FillRect( &tR, &qd.gray);
  210.  
  211.         cntH = 14;
  212.         tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  213.         tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  214.         tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*15 +2;
  215.         tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*16 -1;
  216.         FillRect( &tR, &qd.gray);
  217.  
  218.         cntH = 15;
  219.         tR.left = mFrameLocation.h+ kcgHOffset +kcgHSpacing*cntH +2;
  220.         tR.right = mFrameLocation.h+ kcgHOffset +kcgHSpacing*(1+cntH) -1;
  221.         tR.top =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*15 +2;
  222.         tR.bottom =  mFrameLocation.v+ kcgVOffset+ kcgVSpacing*16 -1;
  223.         FillRect( &tR, &qd.gray);
  224.  
  225.     }
  226.     
  227.     PenNormal();
  228.  
  229.     for ( cntV=0; cntV<16; cntV++) {
  230.         for ( cntH=0; cntH<16; cntH++) {
  231.             MoveTo( mFrameLocation.h+ kcgHOffset+ kcgHSpacing*cntH +4,
  232.                          mFrameLocation.v+ kcgVOffset+ kcgVSpacing*(cntV+1) -3);
  233.             if ( mBaseChar == 0) {
  234.                 // simple ascii. Use our shorts, make sure we get only 1 byte
  235.                 DrawChar( 0x00FF & mChars[ 16*cntV + cntH]);
  236.             } else {
  237.                 // multi-byte
  238.                 DrawText( mChars, (16*cntV + cntH)*sizeof(short), 2);
  239.             }
  240.         }
  241.     }
  242.     
  243.     return;
  244. }
  245.  
  246.  
  247. //***********************
  248. //    DoPageUp, DoPageDown
  249. //
  250. //        We get a msg that the user wants to change the char range. 
  251. //    Use knowledge about Script Systems to only change to char ranges that 
  252. //    make sense.
  253. //****************************************************************************
  254.  
  255. void CLG_CharsGrid::DoPageUp()
  256. {    
  257.     if ( mCurScriptNum == smJapanese) {
  258.         if ( mBaseChar < MAX_KanjiLeadByte) {
  259.             do { mBaseChar++;
  260.             } while ( !KanjiIsLeadByte(mBaseChar));
  261.             FillCodePts();
  262.             this->Refresh();
  263.         } else if ( mBaseChar == MAX_KanjiLeadByte) {
  264.             mBaseChar = 0;
  265.             FillCodePts();
  266.             this->Refresh();
  267.         } else {
  268.             SysBeep(1);
  269.         }
  270.     } else {
  271.         if ( mBaseChar == 0)             mBaseChar = 128;
  272.         else if ( mBaseChar == 255)     mBaseChar = 0;
  273.         else                             mBaseChar++;
  274.         
  275.         FillCodePts();
  276.         this->Refresh();
  277.     }
  278. }
  279. //***********************
  280.  
  281. void CLG_CharsGrid::DoPageDown()
  282. {
  283.     if ( mCurScriptNum == smJapanese) {
  284.         if ( mBaseChar > MIN_KanjiLeadByte) {
  285.             do { mBaseChar--;
  286.             } while ( !KanjiIsLeadByte(mBaseChar));
  287.             FillCodePts();
  288.             this->Refresh();
  289.         } else if ( mBaseChar == MIN_KanjiLeadByte) {
  290.             mBaseChar = 0;
  291.             FillCodePts();
  292.             this->Refresh();
  293.         } else {
  294.             SysBeep(1);
  295.         }
  296.     } else {
  297.         if ( mBaseChar == 0)             mBaseChar = 255;
  298.         else if ( mBaseChar == 128)     mBaseChar = 0;
  299.         else                             mBaseChar--;
  300.  
  301.         FillCodePts();
  302.         this->Refresh();
  303.     }
  304. }
  305.  
  306.  
  307. //***********************
  308. //    DoChangeChars
  309. //****************************************************************************
  310. void
  311. CLG_CharsGrid::DoChangeChars()
  312. {
  313.     short cnt;
  314.     
  315.     long scripNum = GetScriptManagerVariable( smKeyScript);
  316.     
  317.     // re-init the array of chars so no ScriptMgr actions accumulate
  318.     BlockMoveData( mCodePts, mChars, sizeof(unsigned short)*256);
  319.     
  320.     // perform the script action for each char in the array
  321.     //  if we have DblByte data, use different Ptr arithmatic
  322.     if ( mScriptAction != saNoChange) {
  323.         for ( cnt=0;cnt<256;cnt++) {
  324.             if ( mBaseChar == 0) {
  325.                 // simple ascii
  326.                 switch (mScriptAction) {
  327.                     case saUppercase:        
  328.                         UppercaseText( ((Ptr)mChars) +cnt*2 +1, 1, scripNum);
  329.                         break;
  330.                     case saLowercase:    
  331.                         LowercaseText( ((Ptr)mChars) +cnt*2 +1, 1, scripNum);
  332.                         break;
  333.                     case saStripDiacritical:        
  334.                         StripDiacritics( ((Ptr)mChars) +cnt*2 +1, 1, scripNum);
  335.                         break;
  336.                     case saUpperStripDiacritical:    
  337.                         UppercaseStripDiacritics( ((Ptr)mChars) +cnt*2 +1, 1, scripNum);
  338.                         break;
  339.                 };    
  340.             } else {
  341.                 switch (mScriptAction) {
  342.                     case saUppercase:        
  343.                         UppercaseText( ((Ptr)mChars) +cnt*2, 2, scripNum);
  344.                         break;
  345.                     case saLowercase:    
  346.                         LowercaseText( ((Ptr)mChars) +cnt*2, 2, scripNum);
  347.                         break;
  348.                     case saStripDiacritical:        
  349.                         StripDiacritics( ((Ptr)mChars) +cnt*2, 2, scripNum);
  350.                         break;
  351.                     case saUpperStripDiacritical:    
  352.                         UppercaseStripDiacritics( ((Ptr)mChars) +cnt*2, 2, scripNum);
  353.                         break;
  354.                 };    
  355.             }
  356.         }
  357.     }
  358.     
  359.     this->Refresh();
  360.  
  361.     return;
  362. }
  363.  
  364. //*****************************************************************************
  365.